AP Computer Science: Designing Classes – The Menu Class

 

A menu object keeps track of a title, menu items and a prompt. See the example below.

 

Number Types

 

1.     Integer

2.     Decimal

3.     Fraction

4.     Percentage

 

Enter the number of your choice (1-4):

 

The handling of this data will be done by the Menu class, as described below.

 

Data Members

private String title; // title to be displayed above menu

private String prompt; // the prompt to be displayed below the menu

private ArrayList<String> items; // list of items to be shown in menu

private int numItems; // number of items in menu

private int choice; // number entered by user in response to menu

 

Constructor

public Menu()

// Creates an empty menu

 

Accessor Methods

public String getTitle()

// Returns the title of the menu.

 

public String getPrompt()

// Returns the prompt of the menu.

 

public int getNumItems()

// Returns the number of items in the menu.

 

public int getChoice()

// Returns the number of the user’s choice.

 

Modifier Methods

public void setTitle(String t)

// Sets the title to the value in String t

 

public void setPrompt(String p)

// Sets the prompt to the value in String p

 

public void addItem(String item)

// Adds the String item to the end of the ArrayList items

 

public void setChoice(int c)

// Sets the choice number to the int c

 

 

Additional Method

public void display()

// Displays the entire contents of the menu: title, items and prompt. The items

// are preceded by an integer from 1 to numItems as shown in the example below.

 

Number Types

 

1.     Integer

2.     Decimal

3.     Fraction

4.     Percentage

 

Enter the number of your choice (1-4):

 

 

 

Use the file MenuTest.java to test out your class. The file is located in the Working with Classes folder.